package v1

import (
	"gin-wave-im/model/request"
	"gin-wave-im/model/response"
	"gin-wave-im/service"
	"gin-wave-im/util"
	"github.com/gin-gonic/gin"
)

// @Tags Message
// @Summary 获得历史消息
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.HistoryRecord true "对方id, 私聊/群聊, 页数"
// @Success 200 {string} string "{"status_code":200,"data":[],"message":"获取历史记录成功！"}"
// @Router /v1/message/historyRecord [get]
func HistoryRecord(ctx *gin.Context) {
	userId := service.GetUserID(ctx)

	dst := &request.HistoryRecord{}
	if err := ctx.ShouldBindQuery(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}

	messages,err := service.HistoryRecord(userId, dst.DstId, dst.Cmd, dst.Page)
	if err != nil {
		response.Fail(ctx, -1, "获取历史记录失败")
	} else {
		response.Success(ctx, messages, "获取历史记录成功！")
	}
}

// @Tags Message
// @Summary 将消息设为已读
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.MakeRead true "对方id, 私聊/群聊"
// @Success 200 {string} string "{"status_code":200,"data":[],"message":"获取历史记录成功！"}"
// @Router /v1/message/makeRead [post]
func MakeRead(ctx *gin.Context) {
	userId := service.GetUserID(ctx)

	dst := &request.MakeRead{}
	if err := ctx.ShouldBindJSON(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}

	err := service.MakeRead(userId, dst.DstId, dst.Cmd)
	if err != nil {
		response.Fail(ctx, -1, "消息设为已读失败！")
	} else {
		response.Success(ctx, nil, "消息设为已读成功！")
	}
}

// @Tags Message
// @Summary 获得实时消息
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"status_code":200,"data":[],"message":"获取实时消息成功！"}"
// @Router /v1/message [get]
func RealTimeMessage(ctx *gin.Context) {
	userId := service.GetUserID(ctx)
	messages,err := service.RealTimeMessage(userId)
	if err != nil {
		response.Fail(ctx, -1, "获取实时消息失败！")
	} else {
		response.Success(ctx, messages, "获取实时消息成功！")
	}
}